package starter.data;

import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoginTestCases {

	int startSize;
	
	public void runTestCases(ArrayList<String> codeInjection,
			int codeInjectionSize) throws InterruptedException {
		ArrayList<String> webElements = new ArrayList<String>();
		webElements.add("email");
		webElements.add("password");
		codeInjections(codeInjection, codeInjectionSize, webElements);
		emptyAllFields(webElements);
		emptyFieldEmail(webElements);
		emptyFieldPassword(webElements);
		codeInjectionsEmailIsTypeEmail(webElements, codeInjectionSize, webElements);
		loginSuccesfull(webElements);
	}

	private void loginSuccesfull(ArrayList<String> webElements)
			throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.snapsation.com/login");
		WebElement email = driver.findElement(By.name(webElements.get(0)));
		email.sendKeys("damciodamcio666@gmail.com");
		WebElement password = driver.findElement(By.name(webElements.get(1)));
		password.sendKeys("admin123admin");
		email.submit();
		Thread.sleep(1800);
	}

	private void emptyFieldPassword(ArrayList<String> webElements)
			throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.snapsation.com/login");
		WebElement email = driver.findElement(By.name(webElements.get(0)));
		email.sendKeys("damczechowski@gmail.com");
		WebElement password = driver.findElement(By.name(webElements.get(1)));
		password.sendKeys("");
		email.submit();
		Thread.sleep(1800);
	}

	private void emptyFieldEmail(ArrayList<String> webElements)
			throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.snapsation.com/login");
		WebElement email = driver.findElement(By.name(webElements.get(0)));
		email.sendKeys("");
		WebElement password = driver.findElement(By.name(webElements.get(1)));
		password.sendKeys("admin123");
		email.submit();
		Thread.sleep(1800);
	}

	private void emptyAllFields(ArrayList<String> webElements)
			throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.snapsation.com/login");
		WebElement email = driver.findElement(By.name(webElements.get(0)));
		email.sendKeys("");
		WebElement password = driver.findElement(By.name(webElements.get(1)));
		password.sendKeys("");
		email.submit();
		Thread.sleep(1800);
	}

	private void codeInjections(ArrayList<String> codeInjection,
			int codeInjectionSize, ArrayList<String> webElements)
			throws InterruptedException {
		for(startSize = 0; startSize < codeInjectionSize; startSize++){
			WebDriver driver = new FirefoxDriver();
			driver.get("https://www.snapsation.com/login");
			WebElement email = driver.findElement(By.name(webElements.get(0)));
			email.sendKeys(codeInjection.get(startSize));
			WebElement password = driver.findElement(By.name(webElements.get(1)));
			password.sendKeys(codeInjection.get(startSize));
			email.submit();
			Thread.sleep(1800);
		}
	}
	
	private void codeInjectionsEmailIsTypeEmail(ArrayList<String> codeInjection,
			int codeInjectionSize, ArrayList<String> webElements)
			throws InterruptedException {
		for(startSize = 1; startSize < codeInjectionSize; startSize++){
			WebDriver driver = new FirefoxDriver();
			driver.get("https://www.snapsation.com/login");
			WebElement email = driver.findElement(By.name(webElements.get(0)));
			email.sendKeys("test@test.com");
			WebElement password = driver.findElement(By.name(webElements.get(1)));
			password.sendKeys(codeInjection.get(startSize));
			email.submit();
			Thread.sleep(1800);
		}
	}

}